package com.yuanfuxing.boxuegu.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.yuanfuxing.boxuegu.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;



public class LoginActivity extends AppCompatActivity {
        TextView tv_register;
        EditText username,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();
        username = (EditText) findViewById(R.id.et_user_name);
    }
    public void backs(View view){
        finish();
    }
    private void init() {
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_register.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    public void login(View view){
        username = (EditText) findViewById(R.id.et_user_name);
        password = (EditText) findViewById(R.id.et_psw);
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Login");
        params.addQueryStringParameter("username", username.getText().toString());
        params.addQueryStringParameter("password", password.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Loginmodel loginmodel = gson.fromJson(result,Loginmodel.class);
                if(loginmodel.code==200){
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    String userName = username.getText().toString();
                    SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("islogin",true);
                    editor.putString("userName",userName);
                    editor.commit();
                    finish();

                }else {
                    Toast.makeText(LoginActivity.this,"登陆失败"+loginmodel.msg,Toast.LENGTH_SHORT).show();
                }
                Log.i("LoginActivity","onSuccess result"+loginmodel.msg);
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("LoginActivity","onError isOnCallback"+ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }

}
